package com.gimi.cloud.bbp.app.consumer.service.moreInfo.impl;

import com.gimi.cloud.bbp.app.consumer.feign.MoreInfoFeignService;
import com.gimi.cloud.bbp.app.consumer.service.moreInfo.MoreInfoService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.params.SocialSecurityParam;
import com.gimi.cloud.bbp.dto.params.TaobaoParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auther loongs
 * @date 2018/1/9  10:05
 */
@Service
public class MoreInfoServiceImpl implements MoreInfoService {
    private MoreInfoFeignService moreInfoFeignService;
    private static Logger logger = LoggerFactory.getLogger(MoreInfoServiceImpl.class);
    @Autowired
    public MoreInfoServiceImpl(MoreInfoFeignService moreInfoFeignService) {
        this.moreInfoFeignService = moreInfoFeignService;
    }

    @Override
    public void getZmInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取芝麻信用，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.MoreInfoDTO moreInfoDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.MoreInfoDTO.class);
        if(StringUtil.isBlank(moreInfoDTO.getOpenId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        moreInfoDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getZmInfo(moreInfoDTO), httpServletResponse);
    }

    @Override
    public void taobaoLogin(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取淘宝信用，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        TaobaoParams taobaoParams = JsonUtil.jsonToBean(requestData,TaobaoParams.class);
        Util.sendResponse(moreInfoFeignService.taobaoLogin(taobaoParams), httpServletResponse);
    }

    @Override
    public void getSalaryCardInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取工资卡信息，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        Util.sendResponse(moreInfoFeignService.getSalaryCardInfo(), httpServletResponse);
    }

    @Override
    public void getSupportedCreditcardBanks(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡支持银行列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardBankcodeDTO creditcardBankcodeDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardBankcodeDTO.class);
        Util.sendResponse(moreInfoFeignService.getSupportedCreditcardBanks(creditcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void getCreditcardLoginParameter(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡银行登陆参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardBankcodeDTO creditcardBankcodeDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardBankcodeDTO.class);
        Util.sendResponse(moreInfoFeignService.getCreditcardLoginParameter(creditcardBankcodeDTO), httpServletResponse);
    }

    @Override
    public void subCreditcardBasicParam(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("提交蜂巢信用卡基本登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardLoginDTO creditcardLoginDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.CreditcardLoginDTO.class);
        Util.sendResponse(moreInfoFeignService.subCreditcardBasicParam(creditcardLoginDTO), httpServletResponse);
    }

    @Override
    public void getFengchaoEmailList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取蜂巢信用卡支持的邮箱列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.FengchaoEmailDTO fengchaoEmailDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.FengchaoEmailDTO.class);
        Util.sendResponse(moreInfoFeignService.getFengchaoEmailList(fengchaoEmailDTO), httpServletResponse);
    }

    @Override
    public void getFengchaoPositionList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("蜂巢邮箱支持的Position列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.FengchaoPositionDTO fengchaoPositionDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.FengchaoPositionDTO.class);
        Util.sendResponse(moreInfoFeignService.getFengchaoPositionList(fengchaoPositionDTO), httpServletResponse);
    }


    @Override
    public void getProvidentFundInfoSupportedCityList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金城市登录列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO.class);
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoSupportedCityList(gongjijingDTO), httpServletResponse);
    }
    @Override
    public void getProvidentFundInfoCityLoginParameter(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金地区登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO.class);
        if(StringUtil.isBlank(gongjijingDTO.getRegionId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoCityLoginParameter(gongjijingDTO), httpServletResponse);
    }
    @Override
    public void getProvidentFundInfoImage(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取公积金验证码图片，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO.class);
        if(StringUtil.isBlank(gongjijingDTO.getRegionCityId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfoImage(gongjijingDTO), httpServletResponse);
    }
    @Override
    public void getProvidentFundInfo(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("登录并获取公积金信息，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO gongjijingDTO = JsonUtil.jsonToBean(requestData, com.gimi.cloud.bbp.dto.app.moreInfo.GongjijingDTO.class);
        if(StringUtil.isBlank(gongjijingDTO.getLoginParameter())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        gongjijingDTO.setUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getProvidentFundInfo(gongjijingDTO), httpServletResponse);
    }

    @Override
    public void getSheBaoCityList(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保城市列表，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData,SocialSecurityParam.class);
        Util.sendResponse(moreInfoFeignService.getSheBaoCityList(socialSecurityParam), httpServletResponse);
    }
    @Override
    public void getSheBaoCityLoginParamer(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保登录参数，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData,SocialSecurityParam.class);
        if(StringUtil.isBlank(socialSecurityParam.getCityId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getSheBaoCityLoginParamer(socialSecurityParam), httpServletResponse);
    }
    @Override
    public void getSheBaoCodeImage(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("获取社保验证码图片，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData,SocialSecurityParam.class);
        if(StringUtil.isBlank(socialSecurityParam.getSessionToken())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(moreInfoFeignService.getSheBaoCodeImage(socialSecurityParam), httpServletResponse);
    }
    @Override
    public void getSheBaoLoginAndGrab(String requestData, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) throws MsgException {
        logger.info("登录并抓取社保账号，请求参数srcData:" + JsonUtil.replaceNow(requestData));
        SocialSecurityParam socialSecurityParam = JsonUtil.jsonToBean(requestData,SocialSecurityParam.class);
        if(StringUtil.isBlank(socialSecurityParam.getSessionToken())||StringUtil.isBlank(socialSecurityParam.getArgs())||
                StringUtil.isBlank(socialSecurityParam.getImageCode())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        socialSecurityParam.setOperUserId(StringUtil.toInt(httpServletRequest.getHeader("userId")));
        Util.sendResponse(moreInfoFeignService.getSheBaoLoginAndGrab(socialSecurityParam), httpServletResponse);
    }


}
